﻿using Godot;
using Godot.Collections;
using System;

public class ItemStack
{
    private ItemMateria IM;
    private BlockMaterial BM;
    public String Name;
    public String DispalyName;
    public int Amount;
    public ItemData itemData;
    public ItemStack(){}
    public ItemStack(ItemMateria im, int Amount = 1)
    {
        this.Name = im.Name;
        this.DispalyName = im.DisPlayName;
        this.Amount = Amount;
        if (im.GetItemData != null)
        {
            itemData = im.GetItemData();
        }
    }

    public int SubAmount(int amount)
    {
        Amount -= amount;
        if (Amount <= 0) return Amount;
        return 0;
    }
    public int AddAmount(int amount)
    {
        Amount += amount;
        if (Amount > getMaterial().MaxAmount)
        {
            int a = Amount - getMaterial().MaxAmount;
            Amount = getMaterial().MaxAmount;
            return a;

        }
        return 0;

    }
    public ItemStack SetAmount(int amount)
    {
        if (amount <= 0) return null;
        if (amount > getMaterial().MaxAmount)
        {
            Amount = getMaterial().MaxAmount;
        }
        else
        {
            Amount = amount;
        }
        return this;
    }
    public int getLastAmount()
    {
        return getMaterial().MaxAmount - Amount;
    }
    public bool IsFull()
    {
        return Amount == getMaterial().MaxAmount;
    }
    public ItemMateria getMaterial()
    {
        if (IM == null || IM.Name != Name) IM = Materials.GetItemMateria(Name);
        if (IM == null) GD.PrintErr($"{Name}");
        return IM;
    }
    public BlockMaterial getBlockMaterial()
    {
        if (BM == null || BM.Name != Name) BM = Materials.GetBlockMaterial(Name);
        return BM;
    }
    public Dictionary getDict()
    {
        Dictionary dict = new Dictionary();
        dict.Add("Name", Name);
        dict.Add("DisplayName", DispalyName);
        dict.Add("Amount", Amount);
        if (itemData != null) dict.Add("ItemData", itemData.GetDictionary());
        return dict;
    }
    public String toJson()
    {
        return JSON.Print(getDict());
    }
    public ItemStack JsonTo(Dictionary dict)
    {
        Name = (String)dict["Name"];
        DispalyName = (String)dict["DisplayName"];
        Amount = dict["Amount"].ToInt();
        if (dict.Contains("ItemData"))
        {
            itemData = getMaterial().GetItemData().Parse((Dictionary)dict["ItemData"]);
        }
        return this;
    }
    public ItemStack Copy(int amount = 0)
    {
        if (amount == 0) amount = Amount;
        ItemStack item = new ItemStack(getMaterial(), amount);
        if (this.itemData != null) item.itemData = this.itemData.Copy();
        return item;
    }
    public bool equals(ItemStack item)
    {
        if (item.Name == Name && item.Amount == Amount)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public bool equalMaterial(ItemStack item)
    {
        if (item == null) return false;
        if (item.Name == Name)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public String GetLore()
    {
        if (this.itemData != null && this.itemData is ToolData)
        {
            ToolData td = itemData as ToolData;
            return getMaterial().GetLore() + $"\n耐久:{td.Durable}/{td.Max}";
        }

        return getMaterial().GetLore();
    }
}